package com.heima.wemedia.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.AdChannel;
import com.heima.model.wemedia.dto.ChannelDto;
import com.heima.model.wemedia.entity.WmChannel;
import com.heima.model.wemedia.entity.WmSensitive;
import com.heima.wemedia.dao.WmChannelDao;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 自媒体频道表(WmChannel)表服务实现类
 *
 * @author makejava
 * @since 2022-04-12 16:23:07
 */
@Service("wmChannelService")
public class WmChannelServiceImpl implements WmChannelService {
    @Resource
    private WmChannelDao wmChannelDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmChannel queryById(Long id) {
        return this.wmChannelDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param wmChannel 实例对象
     * @return 实例对象
     */
    @Override
    public WmChannel insert(WmChannel wmChannel) {
        this.wmChannelDao.insert(wmChannel);
        return wmChannel;
    }

    /**
     * 修改数据
     *
     * @param wmChannel 实例对象
     * @return 实例对象
     */
    @Override
    public WmChannel update(WmChannel wmChannel) {
        this.wmChannelDao.update(wmChannel);
        return this.queryById(wmChannel.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.wmChannelDao.deleteById(id) > 0;
    }

    /**
     * 查询频道列表
     *
     * @return
     */
    @Override
    public ResponseResult list() {
        WmChannel wmChannel = new WmChannel();
        wmChannel.setStatus(1);
        List<WmChannel> list = wmChannelDao.queryAll(wmChannel);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult listPage(ChannelDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();
        String name = dto.getName();
        //开启分页查询
        PageHelper.startPage(dto.getPage(), dto.getSize());
        Page<WmSensitive> page = wmChannelDao.queryPage(name);
        ResponseResult result = new PageResponseResult(page.getPages(), page.getPageSize(), (int) page.getTotal());
        result.setData(page.getResult());
        return result;
    }

    /**
     * 保存频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult save(AdChannel adChannel) {
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //封装参数
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel, wmChannel);
        wmChannel.setIsDefault(0);
        wmChannel.setStatus(adChannel.getStatus() ? 1 : 0);
        wmChannel.setCreatedTime(new Date());
        wmChannel.setUpdatedTime(new Date());
        wmChannelDao.insert(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult Adupdate(AdChannel adChannel) {
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //封装参数
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel, wmChannel);
        wmChannel.setIsDefault(0);
        wmChannel.setStatus(adChannel.getStatus() ? 1 : 0);
        wmChannel.setUpdatedTime(new Date());
        wmChannelDao.update(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
